


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: FunctionPersister</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.dormancy.persister</a> ]
</div>

<h1>Coverage Summary for Class: FunctionPersister (at.dormancy.persister)</h1>

<table class="coverageStats">

<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">FunctionPersister</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (8/ 8)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (25/ 25)
  </span>
</td>
</tr>
  <tr>
    <td class="name">FunctionPersister$1</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (2/ 2)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (2/ 2)
  </span>
</td>
  </tr>
  <tr>
    <td class="name">FunctionPersister$2</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (2/ 2)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (2/ 2)
  </span>
</td>
  </tr>
  <tr>
    <td class="name">FunctionPersister$3</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (2/ 2)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (2/ 2)
  </span>
</td>
  </tr>
<tr>
  <td class="name"><strong>total</strong></td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (14/ 14)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (31/ 31)
  </span>
</td>
</tr>
</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2013 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.dormancy.persister;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import at.dormancy.Dormancy;
<i>19</i>&nbsp;import com.google.common.base.Function;
<i>20</i>&nbsp;import com.google.common.collect.Iterables;
<i>21</i>&nbsp;import org.springframework.core.CollectionFactory;
<i>22</i>&nbsp;
<i>23</i>&nbsp;import javax.annotation.Nonnull;
<i>24</i>&nbsp;import javax.annotation.Nullable;
<i>25</i>&nbsp;import javax.inject.Inject;
<i>26</i>&nbsp;import java.util.Collection;
<i>27</i>&nbsp;import java.util.Iterator;
<i>28</i>&nbsp;import java.util.Map;
<i>29</i>&nbsp;
<i>30</i>&nbsp;/**
<i>31</i>&nbsp; * Uses {@link Function Functions} for cloning/merging of {@link Iterable Iterables} containing JPA entities.
<i>32</i>&nbsp; *
<i>33</i>&nbsp; * @author Gregor Schauer
<i>34</i>&nbsp; */
<i>35</i>&nbsp;public class FunctionPersister&lt;E&gt; extends AbstractContainerPersister&lt;Iterable&lt;E&gt;&gt; {
<i>36</i>&nbsp;	@Inject
<i>37</i>&nbsp;	public FunctionPersister(@Nonnull Dormancy dormancy) {
<b class="fc"><i>38</i>&nbsp;		super(dormancy);</b>
<b class="fc"><i>39</i>&nbsp;	}</b>
<i>40</i>&nbsp;
<i>41</i>&nbsp;	@Nullable
<i>42</i>&nbsp;	@Override
<i>43</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>44</i>&nbsp;	public &lt;T extends Iterable&lt;E&gt;&gt; Iterable&lt;E&gt; clone_(@Nullable T dbObj, @Nonnull Map&lt;Object, Object&gt; tree) {
<b class="fc"><i>45</i>&nbsp;		if (dbObj == null || tree.containsKey(dbObj)) {</b>
<b class="fc"><i>46</i>&nbsp;			return (Iterable&lt;E&gt;) tree.get(dbObj);</b>
<i>47</i>&nbsp;		}
<b class="fc"><i>48</i>&nbsp;		Collection&lt;E&gt; container = createContainer(dbObj);</b>
<b class="fc"><i>49</i>&nbsp;		tree.put(dbObj, container);</b>
<i>50</i>&nbsp;
<b class="fc"><i>51</i>&nbsp;		Iterables.addAll(container, Iterables.transform(dbObj, getCloneFunction(dbObj, tree)));</b>
<b class="fc"><i>52</i>&nbsp;		return container;</b>
<i>53</i>&nbsp;	}
<i>54</i>&nbsp;
<i>55</i>&nbsp;	@Nullable
<i>56</i>&nbsp;	@Override
<i>57</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>58</i>&nbsp;	public &lt;T extends Iterable&lt;E&gt;&gt; Iterable&lt;E&gt; merge_(@Nullable T trObj, @Nonnull Map&lt;Object, Object&gt; tree) {
<b class="fc"><i>59</i>&nbsp;		if (trObj == null || tree.containsKey(trObj)) {</b>
<b class="fc"><i>60</i>&nbsp;			return (Iterable&lt;E&gt;) tree.get(trObj);</b>
<i>61</i>&nbsp;		}
<b class="fc"><i>62</i>&nbsp;		Collection&lt;E&gt; container = createContainer(trObj);</b>
<b class="fc"><i>63</i>&nbsp;		tree.put(trObj, container);</b>
<i>64</i>&nbsp;
<b class="fc"><i>65</i>&nbsp;		Iterables.addAll(container, Iterables.transform(trObj, getMergeFunction(trObj, tree)));</b>
<b class="fc"><i>66</i>&nbsp;		return container;</b>
<i>67</i>&nbsp;	}
<i>68</i>&nbsp;
<i>69</i>&nbsp;	@Nullable
<i>70</i>&nbsp;	@Override
<i>71</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>72</i>&nbsp;	public &lt;T extends Iterable&lt;E&gt;&gt; Iterable&lt;E&gt; merge_(@Nullable T trObj, @Nullable T dbObj, @Nonnull Map&lt;Object, Object&gt; tree) {
<b class="fc"><i>73</i>&nbsp;		if (trObj == null || dbObj == null || tree.containsKey(trObj)) {</b>
<b class="fc"><i>74</i>&nbsp;			return (Iterable&lt;E&gt;) tree.get(trObj);</b>
<i>75</i>&nbsp;		}
<b class="fc"><i>76</i>&nbsp;		Collection&lt;E&gt; container = createContainer(trObj);</b>
<b class="fc"><i>77</i>&nbsp;		tree.put(trObj, container);</b>
<i>78</i>&nbsp;
<b class="fc"><i>79</i>&nbsp;		Iterables.addAll(container, Iterables.transform(trObj, getMergeFunction(trObj, dbObj, tree)));</b>
<b class="fc"><i>80</i>&nbsp;		return container;</b>
<i>81</i>&nbsp;	}
<i>82</i>&nbsp;
<i>83</i>&nbsp;	/**
<i>84</i>&nbsp;	 * Returns the {@link Function} used for cloning the given persistent object.
<i>85</i>&nbsp;	 *
<i>86</i>&nbsp;	 * @param dbObj the object to clone
<i>87</i>&nbsp;	 * @param tree the adjacency map to use for traversal
<i>88</i>&nbsp;	 * @return the function
<i>89</i>&nbsp;	 */
<i>90</i>&nbsp;	@Nonnull
<i>91</i>&nbsp;	public &lt;T extends Iterable&lt;E&gt;&gt; Function&lt;E, E&gt; getCloneFunction(@Nonnull T dbObj, @Nonnull final Map&lt;Object, Object&gt; tree) {
<b class="fc"><i>92</i>&nbsp;		return new Function&lt;E, E&gt;() {</b>
<i>93</i>&nbsp;			@Override
<i>94</i>&nbsp;			public E apply(@Nullable E input) {
<b class="fc"><i>95</i>&nbsp;				return dormancy.clone_(input, tree);</b>
<i>96</i>&nbsp;			}
<i>97</i>&nbsp;		};
<i>98</i>&nbsp;	}
<i>99</i>&nbsp;
<i>100</i>&nbsp;	/**
<i>101</i>&nbsp;	 * Returns the {@link Function} used for merging the given transient object.
<i>102</i>&nbsp;	 *
<i>103</i>&nbsp;	 * @param trObj the object to merge
<i>104</i>&nbsp;	 * @param tree the adjacency map to use for traversal
<i>105</i>&nbsp;	 * @return the function
<i>106</i>&nbsp;	 */
<i>107</i>&nbsp;	@Nonnull
<i>108</i>&nbsp;	public &lt;T extends Iterable&lt;E&gt;&gt; Function&lt;E, E&gt; getMergeFunction(@Nonnull T trObj, @Nonnull final Map&lt;Object, Object&gt; tree) {
<b class="fc"><i>109</i>&nbsp;		return new Function&lt;E, E&gt;() {</b>
<i>110</i>&nbsp;			@Override
<i>111</i>&nbsp;			public E apply(@Nullable E input) {
<b class="fc"><i>112</i>&nbsp;				return dormancy.merge_(input, tree);</b>
<i>113</i>&nbsp;			}
<i>114</i>&nbsp;		};
<i>115</i>&nbsp;	}
<i>116</i>&nbsp;
<i>117</i>&nbsp;	/**
<i>118</i>&nbsp;	 * Returns the {@link Function} used for merging the given transient object into the persistent object.
<i>119</i>&nbsp;	 *
<i>120</i>&nbsp;	 * @param trObj the transient object
<i>121</i>&nbsp;	 * @param dbObj the persistent object
<i>122</i>&nbsp;	 * @param tree the adjacency map to use for traversal
<i>123</i>&nbsp;	 * @return the function
<i>124</i>&nbsp;	 */
<i>125</i>&nbsp;	@Nonnull
<i>126</i>&nbsp;	public &lt;T extends Iterable&lt;E&gt;&gt; Function&lt;E, E&gt; getMergeFunction(@Nonnull T trObj, @Nonnull T dbObj, @Nonnull final Map&lt;Object, Object&gt; tree) {
<b class="fc"><i>127</i>&nbsp;		final Iterator&lt;E&gt; iterator = dbObj.iterator();</b>
<b class="fc"><i>128</i>&nbsp;		return new Function&lt;E, E&gt;() {</b>
<i>129</i>&nbsp;			@Override
<i>130</i>&nbsp;			public E apply(@Nullable E input) {
<b class="fc"><i>131</i>&nbsp;				return dormancy.merge_(input, iterator.next(), tree);</b>
<i>132</i>&nbsp;			}
<i>133</i>&nbsp;		};
<i>134</i>&nbsp;	}
<i>135</i>&nbsp;
<i>136</i>&nbsp;	/**
<i>137</i>&nbsp;	 * Creates an empty container of the given type.
<i>138</i>&nbsp;	 *
<i>139</i>&nbsp;	 * @param container the original container
<i>140</i>&nbsp;	 * @return the new container
<i>141</i>&nbsp;	 * @see CollectionFactory#createApproximateCollection(Object, int)
<i>142</i>&nbsp;	 */
<i>143</i>&nbsp;	@Nonnull
<i>144</i>&nbsp;	@Override
<i>145</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>146</i>&nbsp;	protected Collection&lt;E&gt; createContainer(@Nonnull Iterable&lt;E&gt; container) {
<b class="fc"><i>147</i>&nbsp;		return CollectionFactory.createApproximateCollection(container, Iterables.size(container));</b>
<i>148</i>&nbsp;	}
<i>149</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2013-07-31 23:31</div>
</div>
</body>
</html>
